package com.faithbj.device.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.faithbj.device.dao.ReservationDao;
import com.faithbj.device.model.configuration.Pager;
import com.faithbj.device.model.entity.Reservation;

@Repository("reservationDao")
public class ReservationDaoImpl  extends BaseDaoImpl<Reservation, String> implements ReservationDao
{

	@Override
	public Pager findReserveCostByExample(Reservation reservation,Integer pageNumber,Integer pageSize) {
		Pager pager = new Pager();
		List<Reservation> list=findByFilter(reservation,pageNumber,pageSize);
		pager.setResult(list);
		
		pager.setPageNumber(pageNumber);
		pager.setPageSize(pageSize);
		
		Integer count=findCountByExample(reservation);
		pager.setTotalCount((count == null) ? pager.getTotalCount() : count.intValue());// count为null，就从pager对象找总数
	
		return pager;		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Reservation> findByFilter(Reservation filterEntity, Integer pageNumber, Integer pageSize,String ... excludeProperty) {
		Criteria criteria = getSession().createCriteria(Reservation.class);
		
		if (filterEntity != null) {
			Example example =  Example.create(filterEntity).ignoreCase().excludeZeroes().enableLike(MatchMode.ANYWHERE);
			if (excludeProperty != null) {
				for (String exclude : excludeProperty) {
					example.excludeProperty(exclude);
				}
			}

			criteria.add(example);
			
			if(filterEntity.getMember()!=null){
				criteria.createAlias("member", "member");
				
				if(StringUtils.isNotBlank(filterEntity.getMember().getUsername()))
					criteria.add(Restrictions.like("member.username", filterEntity.getMember().getUsername(),MatchMode.ANYWHERE));
				
				if(StringUtils.isNotBlank(filterEntity.getMember().getRealName()))
					criteria.add(Restrictions.like("member.realName", filterEntity.getMember().getRealName(),MatchMode.ANYWHERE));				
			}
			
//			if(filterEntity.getDevice()!=null){
//				criteria.createCriteria("device");
//				
//				if(StringUtils.isNotBlank(filterEntity.getDevice().getPropertyName()))
//					criteria.add(Restrictions.like("propertyName", filterEntity.getDevice().getPropertyName(),MatchMode.ANYWHERE));
//			}			
			
			if (pageNumber != null){
				Integer firstResult=(pageNumber.intValue() - 1) * pageSize.intValue();
				criteria.setFirstResult(firstResult);
			}
				
			if (pageSize != null)
				criteria.setMaxResults(pageSize);
		}
		
		return criteria.list();
	};
	
	@Override
	public Integer findCountByExample(Reservation entity) {
		
		Criteria criteria = getSession().createCriteria(Reservation.class);
		
		if (entity != null) {
			Example example =  Example.create(entity).ignoreCase().excludeZeroes().enableLike(MatchMode.ANYWHERE);
			criteria.add(example).addOrder(Order.desc("createDate"));
			
			if(entity.getMember()!=null)
				criteria.createCriteria("member").add(Example.create(entity.getMember()));
		
			if(entity.getDevice()!=null)
				criteria.createCriteria("device").add(Example.create(entity.getDevice()));
			
			criteria.setProjection(Projections.rowCount());	
		}
		
		Long results = (Long) criteria.uniqueResult();
		
		return results.intValue();
	}


	
	
	
}
